﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class PetrolTransactionsList
    Inherits ObservableCollection(Of PetrolTransactionsClass)

    Dim petrolDB As New IETrackerDataClassesDataContext 'GaragePCDataClassesDataContext ' ' 

    Private Shared list As New PetrolTransactionsList

    Public Shared Function GetList() As PetrolTransactionsList
        Return list
    End Function

    Private Sub New()
        ' Make the constructor private, enforcing the 
        ' "factory" concept -- the only way to create
        ' an instance of this class is by calling
        ' the GetList method.
        AddItems()

    End Sub

    Public Shared Sub Reset()
        list.ClearItems()
        list.AddItems()
    End Sub

    Private Sub AddItems()
        Dim petrolTransactions = From petrol In petrolDB.Petrols.AsEnumerable _
                                 Join vehicle In petrolDB.Vehicles _
                                 On petrol.Vehicle Equals vehicle.MainId _
                                 Join trans In petrolDB.Transactions _
                                 On petrol.TransactionID Equals trans.TransactionID _
                                 Select petrol, vehicle _
                                 , transactionDetails = FormatDateTime(petrol.Date.ToString, DateFormat.ShortDate) & " - " & FormatCurrency(petrol.Amount) _
                                 , vehicleDetails = vehicle.RegoNumber & " - " & vehicle.Make & "/" & vehicle.Model _
                                 , image = IIf(IO.File.Exists("../.." & vehicle.image) Or IO.File.Exists(vehicle.image), vehicle.image, "../../Resources/Classic_66_32.png") _
                                 , transactionID = petrol.TransactionID _
                                 , comment = trans.Comment



        For Each pItem In petrolTransactions.ToList.OrderByDescending(Function(x) x.petrol.Date)
            With pItem

                Add(New PetrolTransactionsClass(.petrol.Date _
                                                , .petrol.Vehicle _
                                                , .petrol.Distance _
                                                , .petrol.Litres _
                                                , .petrol.Amount _
                                                , .petrol.AmountPerLitre _
                                                , .transactionDetails _
                                                , .vehicleDetails _
                                                , .image _
                                                , .transactionID _
                                                , .comment))
            End With

        Next
    End Sub

End Class
